<template>
  <div class="filter-container">
    <!-- 第一行：日期选择、删除状态和自定义过滤条件 -->
    <div class="filter-row">
      <div class="top-section">
        <el-date-picker
          v-if="showDatePicker"
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd"
          clearable
          class="filter-item date-picker"
          @change="handleDateChange"
        />

        <el-select
          v-if="showDeleteFilter"
          v-model="isDelete"
          placeholder="删除状态"
          clearable
          class="filter-item delete-select"
          @change="handleFilter"
        >
          <el-option label="未删除" :value="false" />
          <el-option label="已删除" :value="true" />
        </el-select>

        <!-- 自定义过滤条件插槽 -->
        <slot name="filter-items"></slot>
      </div>
    </div>

    <!-- 第二行：搜索输入框和操作按钮 -->
    <div class="filter-row">
      <div class="bottom-section">
        <el-input
          v-if="showSearchInput"
          v-model="keyword"
          :placeholder="searchPlaceholder"
          class="filter-item search-input"
          clearable
          @keyup.enter.native="handleFilter"
          @clear="handleClearInput"
        />

        <el-button class="filter-item search-btn" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>

        <slot name="action-buttons"></slot>
        <slot name="selection-info"></slot>

        <span class="filter-item total-results-count">
          共 {{ total }} 条结果
        </span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'FilterContainer',
  props: {
    total: {
      type: Number,
      default: 0
    },
    searchPlaceholder: {
      type: String,
      default: '请输入关键词'
    },
    showDeleteFilter: {
      type: Boolean,
      default: true
    },
    showSearchInput: {
      type: Boolean,
      default: true
    },
    showDatePicker: {
      type: Boolean,
      default: true
    },
    defaultDateRange: {
      type: Array,
      default: () => {
        // 获取今天的日期
        const today = new Date()
        const todayStr = today.getFullYear() + '-' + 
                        String(today.getMonth() + 1).padStart(2, '0') + '-' + 
                        String(today.getDate()).padStart(2, '0')
        return [todayStr, todayStr]
      }
    }
  },
  data() {
    return {
      isDelete: false,
      keyword: '',
      dateRange: this.defaultDateRange || []
    }
  },
  methods: {
    handleFilter() {
      this.$emit('search', {
        is_delete: this.showDeleteFilter ? this.isDelete : undefined,
        keyword: this.showSearchInput ? this.keyword : undefined,
        dateRange: this.showDatePicker ? this.dateRange : undefined
      })
    },
    handleDateChange(value) {
      this.dateRange = value
      this.handleFilter()
    },
    handleClearInput() {
      this.keyword = ''
      this.handleFilter()
    },
    // 暴露给父组件的方法，用于触发筛选
    triggerSearch() {
      this.handleFilter()
    }
  },
  mounted() {
    // 组件挂载后触发一次筛选，确保显示默认数据
    this.handleFilter()
  }
}
</script>

<style lang="scss" scoped>
.filter-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.08);
}

.filter-row {
  width: 100%;
  margin-bottom: 16px;
  clear: both;
  display: block;
  
  &:last-child {
    margin-bottom: 0;
  }
}

.top-section,
.bottom-section,
.custom-filter-section {
  display: flex !important;
  flex-direction: row !important;
  align-items: center !important;
  gap: 16px !important;
  width: 100% !important;
  min-height: 48px !important;
  flex-wrap: wrap !important;
  margin: 0 !important;
}

.top-section {
  background-color: #f8f9fa;
  padding: 12px;
  border-radius: 6px;
  border: 1px solid #e9ecef;
  flex-wrap: wrap;
  gap: 12px;
}

.bottom-section {
  padding: 12px;
  background-color: #fff;
  border: 1px solid #e9ecef;
  border-radius: 6px;
  flex-wrap: nowrap !important;
  overflow-x: auto !important;
}

.custom-filter-section {
  padding: 12px;
  background-color: #f1f3f4;
  border-radius: 6px;
  border: 1px solid #e9ecef;
  flex-wrap: wrap !important;
}

.filter-item {
  height: 40px !important;
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

// 日期选择器样式
.date-picker {
  width: 280px !important;
  
  &.el-date-editor--daterange.el-range-editor--medium {
    height: 40px !important;
    width: 280px !important;
  }
  
  :deep(.el-input__inner),
  :deep(.el-range-editor.el-input__inner) {
    height: 40px !important;
    line-height: 40px !important;
    padding: 0 15px;
    border-radius: 6px;
    border: 1px solid #DCDFE6;
    transition: all 0.3s;
    width: 280px !important;
    
    &:focus {
      border-color: #409EFF;
      box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
    }
    
    &:hover {
      border-color: #C0C4CC;
    }
  }
  
  :deep(.el-range-input) {
    height: 38px !important;
    line-height: 38px !important;
    width: 140px !important;
    flex: none !important;
  }
  
  :deep(.el-range-separator) {
    width: 30px !important;
    text-align: center;
    flex: none !important;
  }
  
  :deep(.el-input__suffix) {
    height: 40px;
    display: flex;
    align-items: center;
  }
  
  :deep(.el-input__prefix) {
    height: 40px;
    display: flex;
    align-items: center;
  }
}

// 删除状态选择器样式
.delete-select {
  width: 100px;
  
  // 覆盖Element UI的medium尺寸
  &.el-select--medium {
    height: 40px !important;
  }
  
  :deep(.el-input) {
    height: 40px !important;
    
    &.el-input--medium {
      height: 40px !important;
    }
  }
  
  :deep(.el-input__inner) {
    height: 40px !important;
    line-height: 40px !important;
    padding: 0 30px 0 15px;
    border-radius: 6px;
    border: 1px solid #DCDFE6;
    transition: all 0.3s;
    
    &:focus {
      border-color: #409EFF;
      box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
    }
    
    &:hover {
      border-color: #C0C4CC;
    }
  }
  
  :deep(.el-input__suffix) {
    height: 40px;
    display: flex;
    align-items: center;
  }
}

// 搜索输入框样式
.search-input {
  width: 200px;
  
  // 覆盖Element UI的medium尺寸
  &.el-input--medium {
    height: 40px !important;
  }
  
  :deep(.el-input__inner) {
    height: 40px !important;
    line-height: 40px !important;
    padding: 0 35px 0 15px;
    border-radius: 6px;
    border: 1px solid #DCDFE6;
    transition: all 0.3s;
    
    &:focus {
      border-color: #409EFF;
      box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
    }
    
    &:hover {
      border-color: #C0C4CC;
    }
    
    &::placeholder {
      color: #C0C4CC;
      font-size: 14px;
    }
  }
  
  :deep(.el-input__suffix) {
    height: 40px;
    display: flex;
    align-items: center;
    right: 10px;
  }
  
  :deep(.el-input__clear) {
    color: #C0C4CC;
    font-size: 16px;
    cursor: pointer;
    transition: color 0.3s;
    
    &:hover {
      color: #909399;
    }
  }
}

// 搜索按钮样式
.search-btn {
  height: 40px !important;
  padding: 0 20px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  line-height: 1 !important;
  transition: all 0.3s;
  
  &:hover {
    background-color: #66b1ff;
    border-color: #66b1ff;
  }
  
  i {
    margin-right: 6px;
    font-size: 14px;
  }
}

// 选择信息样式
.selection-info {
  color: #606266;
  font-size: 14px;
  display: flex;
  align-items: center;
  height: 40px;
  padding: 0 12px;
  background-color: #f5f7fa;
  border-radius: 6px;
  border: 1px solid #e4e7ed;
}

// 总结果数样式
.total-results-count {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  height: 40px !important;
  line-height: 1 !important;
  font-size: 14px;
  font-weight: 600;
  color: #409EFF;
  padding: 0 12px;
  background-color: #ecf5ff;
  border-radius: 6px;
  border: 1px solid #d9ecff;
  margin: 0;
  box-sizing: border-box;
}

.selection-count {
  color: #E6A23C;
  font-weight: bold;
  margin: 0 4px;
}

// 统一所有按钮高度样式
:deep(.filter-item) {
  &.el-button {
    height: 40px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    line-height: 1 !important;
    border-radius: 6px !important;
    padding: 0 16px !important;
    font-size: 14px !important;
    transition: all 0.3s !important;
  }
  
  &.upload-button {
    .el-upload {
      height: 40px;
      display: flex;
      align-items: center;
      
      .el-button {
        height: 40px !important;
        line-height: 1 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        border-radius: 6px !important;
      }
    }
  }
}

// 全局覆盖Element UI medium尺寸
:deep(.el-input--medium) {
  height: 40px !important;
  
  .el-input__inner {
    height: 40px !important;
    line-height: 40px !important;
  }
}

:deep(.el-select--medium) {
  height: 40px !important;
  
  .el-input {
    height: 40px !important;
  }
  
  .el-input__inner {
    height: 40px !important;
    line-height: 40px !important;
  }
}

:deep(.el-date-editor--daterange.el-range-editor--medium) {
  height: 40px !important;
  width: 360px !important;
}

// 强制覆盖日期选择器宽度
:deep(.el-date-editor.el-range-editor.el-input__inner.filter-item.date-picker) {
  width: 360px !important;
  min-width: 360px !important;
  
  .el-range-input {
    width: 140px !important;
    flex: none !important;
  }
  
  .el-range-separator {
    width: 30px !important;
    flex: none !important;
  }
}

// 响应式设计
@media (max-width: 768px) {
  .top-section,
  .bottom-section {
    gap: 12px;
  }
  
  .date-picker {
    width: 280px;
  }
  
  .search-input {
    width: 200px;
  }
  
  .delete-select {
    width: 120px;
  }
}
</style>
